/**
 * 
 */
package com.exemplo.flickrsocial.view;

import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.exemplo.flickrsocial.R;
import com.exemplo.flickrsocial.service.ImageDownloadTask;
import com.exemplo.flickrsocial.util.ImageUtils;
import com.exemplo.flickrsocial.util.ImageUtils.DownloadedDrawable;
import com.gmail.yuyang226.flickr.photos.Photo;
import com.gmail.yuyang226.flickr.photos.PhotoList;

/**
 * 
 * @author Douglas
 * 
 */
public class listImageAdapter extends BaseAdapter {

	private Activity activity;
	private PhotoList photos;
	private static LayoutInflater inflater = null;

	public listImageAdapter(Activity a, PhotoList d) {
		activity = a;
		photos = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return photos.size();
	}

	public Object getItem(int position) {

		return position;
	}

	public long getItemId(int position) {

		Photo photo = photos.get(position);

		try {

			Bitmap bitmapImageItem = new ImageDownloadTask().execute(
					photo.getSmallUrl()).get();

			Bundle bundle = new Bundle();
			bundle.putParcelable("bitmap", bitmapImageItem);

			Intent imagePreIntent = new Intent(
					activity.getApplicationContext(), ImagePreview.class)
					.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

			imagePreIntent.putExtras(bundle);

			activity.getApplicationContext().startActivity(imagePreIntent);

		} catch (InterruptedException e) {
			Log.e(getClass().getName(), "Error ao tentar baixar imagem");

		} catch (ExecutionException e) {
			Log.e(getClass().getName(), "Error ao tentar baixar imagem");
		}

		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.row, null);

		TextView title = (TextView) vi.findViewById(R.id.imageTitle);
		ImageView icon = (ImageView) vi.findViewById(R.id.imageIcon);
		Photo photo = photos.get(position);

		title.setText(photo.getTitle());
		if (icon != null) {
			ImageDownloadTask task = new ImageDownloadTask(icon);
			Drawable drawable = new DownloadedDrawable(task);
			icon.setImageDrawable(drawable);
			task.execute(photo.getSmallSquareUrl());

		}

		ImageView countView = (ImageView) vi.findViewById(R.id.viewIcon);
		if (photo.getViews() >= 0) {

			countView.setImageResource(R.drawable.views);
			TextView viewsText = (TextView) vi.findViewById(R.id.viewsText);
			viewsText.setText(String.valueOf(photo.getViews()));

		} else {
			countView.setImageBitmap(null);
		}

		return vi;
	}

}
